package org.example.demo.service.impl;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.vod.upload.impl.UploadVideoImpl;
import com.aliyun.vod.upload.req.UploadStreamRequest;
import com.aliyun.vod.upload.resp.UploadStreamResponse;
import org.example.demo.entity.User;
import org.example.demo.mapper.UserMapper;
import org.example.demo.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.demo.util.ConstantPropertiesUtils;
import org.example.demo.util.ConstantVodUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lining
 * @since 2023-11-28
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
   public String uploadFileAvatar(MultipartFile file){
       String endpoint = ConstantPropertiesUtils.END_POINT;
       String accessKeyId = ConstantPropertiesUtils.ACCESS_KEY_ID;
       String accessKeySecret = ConstantPropertiesUtils.ACCESS_KEY_SECRET;
       String bucketName = ConstantPropertiesUtils.BUCKET_NAME;

       //2.创建OSS实例
       OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

       try {
           //3.获取到将要上传的文件的输入流
           InputStream inputStream = file.getInputStream();

           //4.获取到将要上传的文件的名称
           String fileName = file.getOriginalFilename();

           //5.调用oss的方法实现上传
           ossClient.putObject(bucketName, fileName, inputStream);

           //6.关闭OSS对象
           ossClient.shutdown();

           //7.拼接路径
           String url = "https://" + bucketName + "." + endpoint + "/" + fileName;
           return url;
       } catch (Exception e) {
           e.printStackTrace();
           return null;
       }
   }
    public String uploadAlyVideo(MultipartFile file){
        try {
            //上传的文件的原始名称(如:01.mp4)
            String fileName = file.getOriginalFilename();
            //上传后在阿里云显示的名称(带不带后缀都行,我这里没带)
            String title = fileName.substring(0, fileName.lastIndexOf("."));
            //上传的文件的输入流
            InputStream inputStream = file.getInputStream();

            UploadStreamRequest request = new UploadStreamRequest(
                    ConstantVodUtils.ACCESS_KEY_ID,
                    ConstantVodUtils.ACCESS_KEY_SECRET,
                    title,
                    fileName,
                    inputStream);
            UploadVideoImpl uploader = new UploadVideoImpl();
            UploadStreamResponse response = uploader.uploadStream(request);

            String videoId = null;
            if (response.isSuccess()) {
                videoId = response.getVideoId();
            } else { //如果设置回调URL无效，不影响视频上传，可以返回VideoId同时会返回错误码。其他情况上传失败时，VideoId为空，此时需要根据返回错误码分析具体错误原因
                videoId = response.getVideoId();
            }

            return videoId;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }








}
